package com.yxw.bamboo.core.database.sql;

import com.yxw.bamboo.api.dao.MockDataMapper;
import com.yxw.bamboo.core.BytesHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 维护mock数据
 * 数据来源通过任务将数据库或者配置文件中的mock配置数据推送进来
 */
@Component
public class Mock {

    @Autowired
    MockDataMapper mockDataMapper;
    public static final List<MockData> MOCK_LIST=new ArrayList<MockData>();

    private volatile boolean isSearching=false;

   static {
//        MOCK_LIST.add(new MockData(false,"mysql",
//                "Select id,name from [user]",
//                "select 1 as ID,'12211' as NAME union select 2 as ID,'尼玛' as NAME","yxweee","userdb"));
//
//
//
//        //SELECT * FROM [user] where name=@P0 and id=@P1
//        MOCK_LIST.add(new MockData(false,"mssql",
//                "SELECT * FROM [user] where name=@P0 and id=@P1",
//                "select 1 as ID,'12211' as NAME union select 2 as ID,'尼玛' as NAME","yxweee","userdb"));
//        //SELECT * FROM [user] where name=@P0 and id=@P1
//        MOCK_LIST.add(new MockData(false,"mssql",
//                "SELECT * FROM [user]",
//                "select 1 as ID,'12211' as NAME union select 2 as ID,'尼玛' as NAME","yxweee","userdb"));
//        MOCK_LIST.add(new MockData(false,"mssql",
//                "select id,name from [user] where id=@P0",
//                "select 1 as ID,'12211' as NAME union select 2 as ID,'尼玛' as NAME","yxweee","userdb"));
//
//        MOCK_LIST.add(new MockData(false,"mssql",
//                "yxwtest",
//                "select 1 as ID,'12211' as NAME union select 2 as ID,'尼玛' as NAME","yxweee","userdb"));
//
//        //yxwtesto555555558888
//        MOCK_LIST.add(new MockData(false,"mssql",
//                "yxwtesto555555558888",
//                "select 1 as ID,'12211' as NAME union select 2 as ID,'尼玛' as NAME","yxweee","userdb"));

    }

    public static synchronized void add(MockData mockData)
    {
        MOCK_LIST.add(mockData);
    }
    public static synchronized void add(List<MockData> mockDataList)
    {
        MOCK_LIST.addAll(mockDataList);
    }
    public static synchronized void remove(MockData mockData)
    {
        MOCK_LIST.remove(mockData);
    }

    /**
     * 是否命中
     */
    public static MockData hit(String sql,String userName,String dbName) {

        MockData mockResult = null;
        String currentCompereSql = sql.toLowerCase().replaceAll(" ", "");
        System.out.println("目标sql:" + currentCompereSql);

        if(userName!=null&&dbName!=null)
        {

            for (MockData mockData : MOCK_LIST) {
                String mockSql = mockData.getMockSql();
                if(userName.trim().equals(mockData.getUserName().trim())
                        &&dbName.trim().equals(mockData.getDbName().trim()))
                {

                    String mockCompareSql = mockSql.toLowerCase().replaceAll(" ", "");

                    if (currentCompereSql.equals(mockCompareSql)) {
                        mockResult = mockData;
                        System.out.println("mocksql:" + mockCompareSql);
                        break;
                    }
                }

            }
        }

        return mockResult;
     }

    /**
     * 是否命中
     */
    public static MockData hitMsSql(String sql,String userName,String dbName) {

        MockData mockResult = null;
        String currentCompereSql = sql.toLowerCase().replaceAll(" ", "");
        System.out.println("目标sql:" + currentCompereSql);

        if(dbName!=null)
        {

            for (MockData mockData : MOCK_LIST) {
                String mockSql = mockData.getMockSql();

               String tempDb= mockData.getDbName().trim();

                tempDb=new String(BytesHelper.toUCS16("'"+tempDb+"'")).trim();

                if(dbName.toLowerCase().trim().equals(tempDb.toLowerCase().trim()))
                {
                    String mockCompareSql = mockSql.toLowerCase().replaceAll(" ", "");
                    if (currentCompereSql.equals(mockCompareSql)) {
                        mockResult = mockData;
                        System.out.println("mocksql:" + mockCompareSql);
                        break;
                    }
                }

            }
        }

        return mockResult;
    }

    /**
     * 从数据库中拉取mock data 填充到list中
     */
    public  void refresh()
    {
        //防止多个同时查询
        if(isSearching)
        {
            return;

        }
        isSearching=true;
        try {
            Integer maxIndex=0;
            if(Mock.MOCK_LIST.size()>0)
            {
                MockData mockData= Mock.MOCK_LIST.stream().max((p,p1)->p.getId() -p1.getId()).get();
                if(mockData!=null)
                {
                    maxIndex=mockData.getId();
                }
            }
            List<MockData> mockDataList= mockDataMapper.findAllMockData(maxIndex);
            if(mockDataList!=null&&mockDataList.size()>0)
            {
                MOCK_LIST.addAll(mockDataList);
            }

        }finally {

            isSearching=false;
        }

    }

}
